package com.avic.rong.course.activity.otherActivity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.widget.ImageView;

import com.avic.rong.R;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.StatusBarUtil;
import com.bumptech.glide.Glide;
import com.example.minato.minastore.utils.AppUtils;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import butterknife.BindView;

public class VideoPlayerActivity extends AppCompatActivity {

    public static final String ARG_NAME = "name";
    public static final String ARG_URL = "url";
    public static final String ARG_IMAGE_URL = "image_url";

    @BindView(R.id.texture_view)
    TextureView textureView;

    private StandardGSYVideoPlayer videoPlayer;
    private OrientationUtils orientationUtils;
    private String title = ConstantsUtils.EMPTY_STR;
    private String videoUrl = ConstantsUtils.EMPTY_STR;
    private String firstImageUrl = ConstantsUtils.EMPTY_STR;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        title = getIntent().getStringExtra(ARG_NAME);
        videoUrl = getIntent().getStringExtra(ARG_URL);
        firstImageUrl = getIntent().getStringExtra(ARG_IMAGE_URL);

        //设置状态栏颜色
        StatusBarUtil.setStatusBarColor(this, getResources().getColor(R.color.black));

        init();
    }

    private void init() {
        videoPlayer = (StandardGSYVideoPlayer) findViewById(R.id.detail_player);

        Log.e("cjx", "--------------");
        Log.e("cjx", "videoPath:" + videoUrl);
        Log.e("cjx", "firstImgPath:" + firstImageUrl);

        String source = null;
        if (TextUtils.isEmpty(videoUrl)) return;
        if (videoUrl.startsWith("http")) {
            source = videoUrl;
        } else {
            source = AppUtils.COURSE_VIDEO_URL + videoUrl;
        }

        videoPlayer.setUp(source, true, "测试视频");

        //增加封面
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(VideoPlayerActivity.this).load(firstImageUrl).placeholder(R.mipmap.common_bg).into(imageView);
        videoPlayer.setThumbImageView(imageView);
        //增加title
        videoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
        videoPlayer.getTitleTextView().setText(title);
        //设置返回键
        videoPlayer.getBackButton().setVisibility(View.VISIBLE);
        //设置旋转
        orientationUtils = new OrientationUtils(this, videoPlayer);
        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                orientationUtils.resolveByClick();
            }
        });
        //是否可以滑动调整
        videoPlayer.setIsTouchWiget(true);
        //设置返回按键功能
        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        videoPlayer.startPlayLogic();
    }


    @Override
    protected void onPause() {
        super.onPause();
        videoPlayer.onVideoPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        videoPlayer.onVideoResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }

    @Override
    public void onBackPressed() {
        //先返回正常状态
        if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            videoPlayer.getFullscreenButton().performClick();
            return;
        }
        //释放所有
        videoPlayer.setVideoAllCallBack(null);
        super.onBackPressed();
    }


    public static Intent getIntent(Context context, String name, String url, String imageUrl) {
        Intent intent = new Intent(context, VideoPlayerActivity.class);
        intent.putExtra(ARG_NAME, name);
        intent.putExtra(ARG_URL, url);
        intent.putExtra(ARG_IMAGE_URL, imageUrl);
        return intent;
    }


}
